<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="人员" prop="ownerIds">
        <el-select
          v-model="queryParams.ownerIds"
          placeholder="请选择人员"
          :clearable="true"
          multiple
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <Table :columns="columns" :adaptive="true" :data="queryData" height-per="90" />
  </ContentWrap>
</template>
<script setup name="DealConvert" lang="ts">
import * as UserApi from '@/api/system/user/user'
import * as StatisticsCustomerApi from '@/api/statistics/statisticsCustomer'

const userList = ref<UserApi.UserVO[]>([])
const queryFormRef = ref()
const queryParams = ref({
  ownerIds: undefined
})
const queryData = ref()
const columns = [
  {
    label: '人员',
    prop: 'ownerId',
    formatter: ({ ownerId }) => userList.value.find((obj) => obj.id === ownerId)?.nickname
  },
  {
    label: '成交客户数',
    prop: 'dealCount'
  },
  {
    label: '总客户数',
    prop: 'totalCount'
  },
  {
    label: '转化率',
    prop: 'dealRate',
    formatter: ({ dealCount, totalCount }) => ((dealCount / totalCount) * 100).toFixed(2) + '%'
  }
]
/** 初始化 */
onMounted(async () => {
  userList.value = await UserApi.getOwnerUser()

  handleClickReset()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await StatisticsCustomerApi.dealConvertQuery(queryParams.value)
}
</script>
